package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.domain.SysParking;
import com.ruoyi.system.domain.SysPreferential;
import com.ruoyi.system.domain.parm.SysPreferentialParm;
import com.ruoyi.system.exception.GlobalSysException;
import com.ruoyi.system.mapper.SysPreferentialMapper;
import com.ruoyi.system.service.ISysBusinessService;
import com.ruoyi.system.service.ISysParkingService;
import com.ruoyi.system.service.ISysPreferentialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * 商家价格配置Controller
 * 
 * @author ruoyi
 * @date 2024-03-31
 */
@Api(tags = "商家价格配置")
@RestController
@RequestMapping("/bus/preferential")
public class BusPreferentialController extends BaseController
{
    @Autowired
    private ISysPreferentialService sysPreferentialService;
    @Autowired
    private ISysBusinessService sysBusinessService;
    @Autowired
    private SysPreferentialMapper sysPreferentialMapper;

    @Autowired
    private ISysParkingService sysParkingService;
    /**
     * 查询商家价格配置列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "商家价格配置list")
    public TableDataInfo list(SysPreferential sysPreferential)
    {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new GlobalSysException("用户未登陆");
        }
        sysPreferential.setBusinessId( bu.getId());

        startPage();
        List<SysPreferential> list = sysPreferentialService.selectSysPreferentialList(sysPreferential);
        return getDataTable(list);
    }



    /**
     * 获取商家价格配置详细信息
     */
    @GetMapping(value = "/info")
    @ApiOperation(value = "商家价格配置info")
    public AjaxResult getInfo(@RequestParam(name = "id",required = true) String id)
    {
        return success(sysPreferentialService.selectSysPreferentialById(id));
    }

    /**
     * 新增商家价格配置
     */
    @PostMapping("add")
    @ApiOperation(value = "商家价格配置添加")
    public AjaxResult add(@RequestBody @Valid SysPreferentialParm sysPreferentialParm)
    {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new GlobalSysException("用户未登陆");
        }

        SysPreferential sysPreferential = new SysPreferential();
        BeanUtils.copyProperties(sysPreferentialParm, sysPreferential);

        sysPreferential.setBusinessId( bu.getId());

        // 查询价格是否用重合
        // 开始时间是否有重合
        List<SysPreferential> isHas1 = sysPreferentialMapper.selectSysPreferentialByGreaterThan(sysPreferential.getGreaterThan(),bu.getId());
        // 结束时间是否有重合
        List<SysPreferential> isHas2 = sysPreferentialMapper.selectSysPreferentialByLessThan(sysPreferential.getLessThan(),bu.getId());

        if(isHas1.size()>0 || isHas2.size()>0){
            throw new GlobalSysException("定义小时范围有重合请更正");

        }

        SysParking parking = sysParkingService.selectSysParkingById(sysPreferential.getParkingId());
        if(parking ==null || !parking.getBusinessId().equals(sysPreferential.getBusinessId())){

            throw new GlobalSysException("停车场id无效");
        }
        sysPreferential.setId(UUID.randomUUID().toString());
        return toAjax(sysPreferentialService.insertSysPreferential(sysPreferential));
    }

    /**
     * 修改商家价格配置
     */
    @PutMapping("edit")
    @ApiOperation(value = "商家价格配置修改")
    public AjaxResult edit(@RequestBody  @Valid SysPreferentialParm sysPreferentialParm)
    {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new GlobalSysException("用户未登陆");
        }
        SysPreferential sysPreferential = new SysPreferential();
        BeanUtils.copyProperties(sysPreferentialParm, sysPreferential);

        sysPreferential.setBusinessId( bu.getId());

        // 查询价格是否用重合
        // 开始时间是否有重合
        List<SysPreferential> isHas1 = sysPreferentialMapper.selectSysPreferentialByGreaterThan(sysPreferential.getGreaterThan(),bu.getId());
        // 结束时间是否有重合
        List<SysPreferential> isHas2 = sysPreferentialMapper.selectSysPreferentialByLessThan(sysPreferential.getLessThan(),bu.getId());
        if(isHas1.size()>0){
            for (SysPreferential  item:   isHas1){
                if(!item.getId().equals(sysPreferential.getId())){
                    throw new GlobalSysException("定义小时范围有重合请更正");
                }

            }
        }
        if(isHas2.size()>0){
            for (SysPreferential  item2:   isHas2){
                if(!item2.getId().equals(sysPreferential.getId())){
                    throw new GlobalSysException("定义小时范围有重合请更正");
                }

            }
        }
        SysParking parking = sysParkingService.selectSysParkingById(sysPreferential.getParkingId());
        if(parking ==null || !parking.getBusinessId().equals(sysPreferential.getBusinessId())){

            throw new GlobalSysException("停车场id无效");
        }

        return toAjax(sysPreferentialService.updateSysPreferential(sysPreferential));
    }

    /**
     * 删除商家价格配置
     */
	@DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new GlobalSysException("用户未登陆");
        }

        return toAjax(sysPreferentialService.deleteSysPreferentialByIds(ids,bu.getId()));
    }
}
